<?php
namespace Admin\Controller;
use Think\Controller;

/**
 *幻灯片管理*
 *ant-age.com*
 *
 */
class SlideController extends CommonController{

    // 幻灯片
    public function slide() {
        $page_no = I('get.p') ? I('get.p') : 1;
        $page_size = $_GET['page_size'] ? $_GET['page_size'] : 20;
        $MODEL = M("slide");
        $where['del']='0';
        $data = $MODEL->page($page_no, $page_size)->where($where)->select();
        $count = $MODEL->where($where)->count();
         //总记录数
        $Page = new \AntAge\Page($count, $page_size);
         // 实例化分页类 传入总记录数和每页显示的记录数
        $show = $Page->show();
         // 分页显示输出
        $this->assign('page', $show);
         // 赋值分页输出
        $this->assign("count", $count);
        $this->assign("data", $data);
        $this->display();
    }

    // 删除幻灯片
    public function delete() {
        $id['id'] = $_GET['id'];
        $MODEL = M('slide');
        // $del['del']='1';
        $MODEL->where($id)->delete();
        $this->success('删除成功', 'Slide');
    }

    //添加幻灯片
    public function add() {

        if (!IS_POST) {
            $this->display();
        }
        else {
            $image_arr = com_upload();
            $MODEL = D("slide");
            if ($MODEL->create()) {
                $MODEL->image = __ROOT__ .'/Public' .  $image_arr[image];
                $temp = $MODEL->add();
            }else {
                exit($MODEL->getError());
            }

            if ($temp) {
                $this->success('数据添加成功！', 'Slide');
            }else {
                $this->error('数据未插入');
            }
        }
    }

    public function update(){
        $id = I('get.id');
        $data = M('slide')->where("id='$id'")->find();
        // dump($id);exit();
        if ($data['type'] == 0) {
            $data['type'] = '酒吧';
        }
        if ($data['type'] == 1) {
            $data['type'] = 'K歌';
        }
        if ($data['type'] == 2) {
            $data['type'] = '美食';
        }
        $this->assign("data", $data);
        $this->display();
    }

    public function slide_updata() {

        $data = M("index_photos")->select();
        foreach ($data as $key => $value) {
            $list['image'.$key] = $value['photo_id'];
            $list['url'.$key] = $value['url'];
            $list['status'.$key] = $value['status'];
        }
        // dump($list);exit();
        $this->assign("data", $list);
        $this->display();
    }

    public function do_updata() {

        $image_arr = com_upload();

        $MODEL = M('index_photos');

        if ($image_arr[image0]) 
        {
            $dat = $MODEL->where('id=1')->save(array('photo_id' =>__ROOT__ .'/Public'. $image_arr[image0]));
        }
        if ($image_arr[image1]) 
        {
            $dat = $MODEL->where('id=2')->save(array('photo_id' =>__ROOT__ .'/Public' .  $image_arr[image1]));
        }
        if ($image_arr[image2]) 
        {
            $dat = $MODEL->where('id=3')->save(array('photo_id' =>__ROOT__ .'/Public' .  $image_arr[image2]));
        }
        $status0 = I('status0');
        $status1 = I('status1');
        $status2 = I('status2');

        $list = array(
            'status' => $status0,
            'url' => I('url0'),
            );
        $dat1 = $MODEL->where('id=1')->save($list);

        $list1 = array(
            'status' => $status1,
            'url' => I('url1'),
            );
        $dat2 = $MODEL->where('id=2')->save($list1);

        $list2 = array(
            'status' => $status2,
            'url' => I('url2'),
            );
        $dat3 = $MODEL->where('id=3')->save($list2);

        // dump($status);
        if ($dat || $dat3 || $dat2 || $dat1 ) {
            $this->success('成功！', 'Slide_updata');
        }
        else {
            $this->error('数据未插入');
        }
    }

    public function do_update() {
        
        $id = I('post.id');
        
        $image_arr = com_upload();

        $MODEL = M('slide');

        if ($image_arr[image]) 
        {
            $dat = $MODEL->where("id='$id'")->save(array('image' =>__ROOT__ .'/Public'. $image_arr[image]));
        }

        if ($dat) {
            $this->success('成功！', 'Slide');
        }
        else {
            $this->error('数据未插入');
        }
    }


    public function base(){

        if (IS_POST) 
        {
            $image_arr = com_upload();

            $MODEL = M('config_img');

            if ($image_arr[image0]) 
            {
                $dat = $MODEL->where('id=1')->save(array('image' =>__ROOT__ .'/Public'. $image_arr[image0]));
            }
            if ($image_arr[image1]) 
            {
                $dat = $MODEL->where('id=2')->save(array('image' =>__ROOT__ .'/Public' .  $image_arr[image1]));
            }
            if ($image_arr[image2]) 
            {
                $dat = $MODEL->where('id=3')->save(array('image' =>__ROOT__ .'/Public' .  $image_arr[image2]));
            }
            if ($image_arr[image3]) 
            {
                $dat = $MODEL->where('id=4')->save(array('image' =>__ROOT__ .'/Public' .  $image_arr[image3]));
            }
            if ($image_arr[image4]) 
            {
                $dat = $MODEL->where('id=5')->save(array('image' =>__ROOT__ .'/Public' .  $image_arr[image4]));
            }
            if ($image_arr[image5]) 
            {
                $dat = $MODEL->where('id=6')->save(array('image' =>__ROOT__ .'/Public' .  $image_arr[image5]));
                $dats = M('user')->where('id=3')->save(array('photo_id' =>__ROOT__ .'/Public' .  $image_arr[image5]));
            }

            $dat1 = $MODEL->where('id=5')->save(array('val'=>I('val4')));
            $dat2 = $MODEL->where('id=6')->save(array('val'=>I('val5')));
            // dump($status);
            if ($dat || $dat1 || $dat2) {
                $this->success('成功！', 'Base');
            }
            else {
                $this->error('数据未插入');
            }
        }
        else
        {
            $data = M("config_img")->select();

            foreach ($data as $key => $value) {
                $list['image'.$key] = $value['image'];
                $list['val'.$key] = $value['val'];
            }
            // dump($list);exit();
            $this->assign("data", $list);
            $this->display();
        }
    }
}
